import {
	defineStore
} from 'pinia';
import {
	toGetConfessionList,
	toAddConfession,
	toDeleteConfession,
	toGetConfessionListByUserId
} from "@/utils/request/confession.js"
import moment from "moment"
export const useConfessionStore = defineStore('confession', {
	state: () => {
		return {
			status: {
				code: "",
				msg: ""
			},
			deletedStatus: {
				code: "",
				msg: ""
			},
			confessionList: []

		};
	},
	// 也可以这样定义
	// state: () => ({ count: 0 })
	actions: {
		//获取用户所在的学校的表白墙列表
		async getConfessionList(schoolID) {
			try {
				this.confessionList = []
				const res = await toGetConfessionList(schoolID);
				this.status.code = res.code
				this.status.msg = res.msg;
				if (res.code == "200") {
					res.data.forEach((p) => {
						p.addTime = moment(p.addTime).format("YYYY-MM-DD HH:mm:ss")
						if (p.imgSrc != null && p.imgSrc != "") {
							p.imgSrc = p.imgSrc.split(";")
						}
						this.confessionList.unshift(p)
					})
				} else {
					this.confessionList = []
				}
			} catch (err) {
				this.status.code = "405"
				this.status.msg = "网络不好，请稍后重试"
			}
		},

		//发布表白墙
		async addConfession(files, data) {
			try {
				const res = await toAddConfession(files, data);
				this.status.code = res.code
				this.status.msg = res.msg;
			} catch (err) {
				this.status.code = "405"
				this.status.msg = "网络不好，请稍后重试"
			}
		},
		//删除我发布的表白墙
		async deleteConfession(data) {
			try {
				const res = await toDeleteConfession(data);
				this.deletedStatus.code = res.code
				this.deletedStatus.msg = res.msg;
			} catch (err) {
				this.deletedStatus.code = "405"
				this.deletedStatus.msg = "网络不好，请稍后重试;"
			}
		},
		//获取我的表白墙的列表
		async getConfessionListByUserId(userId) {
			try {
				this.confessionList = []
				const res = await toGetConfessionListByUserId(userId);
				this.status.code = res.code
				this.status.msg = res.msg;
				if (res.code == "200") {

					res.data.forEach((p) => {
						p.addTime = moment(p.addTime).format("YYYY-MM-DD HH:mm:ss")
						if (p.imgSrc != null && p.imgSrc != "") {
							p.imgSrc = p.imgSrc.split(";")
						}
						this.confessionList.unshift(p)
					})

				}

			} catch (err) {
				this.status.code = "405"
				this.status.msg = "网络不好，请稍后重试";
			}
		}
	}
});